package org.esprit.ocm.client.ui.ec2;

import org.esprit.ocm.client.rpc.ec2.SecurityGroupService;
import org.esprit.ocm.client.rpc.ec2.SecurityGroupServiceAsync;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

public class AddGroup extends Window {

	private final SecurityGroupServiceAsync	securityGroupService	= GWT.create(SecurityGroupService.class);
	
	public AddGroup(final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {
		setTitle("Add Group");
		setWidth(250);
		setHeight(150);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(true);
		centerInPage();

		final DynamicForm formGroup = new DynamicForm();
		formGroup.setWidth(100);
		formGroup.setHeight(100);
		formGroup.setPadding(5);
		formGroup.setLayoutAlign(VerticalAlignment.TOP);
		
		

		final TextItem name = new TextItem("name");
		name.setTitle("name");
		name.setRequired(true);

		final TextItem description = new TextItem("description");
		description.setTitle("Description");
		description.setRequired(true);

		ButtonItem createButton = new ButtonItem("create", "Create");
		createButton.setAlign(Alignment.CENTER);
		createButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				
				securityGroupService.createSecurityGroup(name.getValue().toString(), description.getValue().toString(), server, credentials, new AsyncCallback<Boolean>() {
					
					public void onSuccess(Boolean arg0) {
						hide();
						SC.say("Group Created");
					}
					
					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						
					}
				});
			}
		});

		formGroup.setFields(name, description, createButton);
		formGroup.redraw();

		addItem(formGroup);

		draw();
	}
}
